require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var userActive=JSON.parse(sessionStorage.getItem("userActive"));
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
if(!userInfo){
    location.href="/loginIndex"
}
console.log(userActive);
var vm = new Vue({
    el:"#circleBar",
    data:{
        isShow:false,
        uid:"",//用户id
        circleId:"",//圈子id
        list:{
            pageNumber:1,
            state:1 //1 最新评论 2最新发表 3精华
        },
        type:"",
        num:"",
        groupInfo:{},
        state:"",
        square:"",
        goodslength:"",
        isScreen:"",
        pop:{
            question: "您确定要删除帖子？",
            show: false
        },
        postId:"",
        circlePost:{},
        circlePost1:[],
        circlePost2:[],
        loadFlag:false,
        commentList:{},
        commentList1:[],
        commentList2:{},
        // postId:"",//帖子id
        commentText:""
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.circleId=this.searchObject&&this.searchObject.circleId;
        this.uid=this.searchObject&&this.searchObject.uid;
        this.num=this.searchObject&&this.searchObject.num;
        this.type=this.searchObject&&this.searchObject.type;
        this.square=this.searchObject&&this.searchObject.square;
        this.getGroupInfo();
    },
    methods:{
        //后退
        getBack: function () {
            if(this.square==1){
                location.href="/friend/circleSquare";
            }else if(this.type==3){
                history.go(-1);
            }else if(this.num==1){
                location.href="/see/selfMediaPage?categoryId="+userActive;
            }else{
                location.href="/friend?indexs=3";
            }
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //圈子贴吧信息
        getGroupInfo:function(){
            var that=this;
            var data={
                loginUid:that.uid,
                circleId:that.circleId
            };
            this.ajax({url:"/friend/getGroupInfo",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.groupInfo=b.json.data;
                    that.goodslength=that.groupInfo.products.length;
                    that.state=that.groupInfo.state;
                    that.getCirclePost();
                }else{
                    vm.$refs.prompt.toast("获取圈子信息失败！")
                }
            });
        },
        //圈子贴吧列表
        getCirclePost:function(){
            var that=this;
            var data=that.list;
            data.loginUid=that.uid;
            data.circleId=that.circleId;
            this.ajax({url:"/friend/getCirclePost",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.circlePost=b.json.data;
                    that.circlePost1=that.circlePost.list;
                    if(that.circlePost.pageNumber>1){
                        if(that.circlePost.list.length>0) {
                            that.circlePost2 = that.circlePost2.concat(b.json.data.list);
                        }
                    }else{
                        that.circlePost2=that.circlePost1;
                    }
                    vm.loadFlag=false;
                }else{
                    that.circlePost={};
                    that.circlePost1=[];
                    that.circlePost2=[];
                    vm.$refs.prompt.toast("获取圈子贴吧列表失败！")
                }
            });
        },
        //通过用户id 加入圈子
        joinGroup:function(){
            var that=this;
            var data={
                loginUid:that.uid,
                circleId:that.circleId
            };
            this.ajax({url:"/friend/joinGroup",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    if(b.json.data=="申请加入!等待圈主审核"){
                        vm.$refs.prompt.toast("申请加入!等待圈主审核！");
                    }else{
                        vm.$refs.prompt.toast("加入成功！");
                        that.state=2;
                    }
                    //that.state=2;
                }else{
                    vm.$refs.prompt.toast("加入圈子失败！");
                }
            });
        },
        getTab:function(num){
            this.list.pageNumber=1;
            this.list.state=num;
            this.getCirclePost();
        },
        getNext:function(type,productId){
            var path;
            if(type==1){
                if(this.groupInfo.uid==userInfo.chatId&&this.groupInfo.userCount<=1){
                    vm.$refs.prompt.toast("自己一个人聊撒！");
                    return false;
                }
                path="/friend?type=2&circleId="+this.circleId+"&uid="+this.uid;
                if(this.type==3){
                    path+="&num=1";
                }
            }else if(type==2){
                path="/friend/circleInfo?circleId="+this.circleId+"&uid="+this.uid;
            }else if(type==3){
                path="/friend/circleProducts?circleId="+this.circleId+"&uid="+this.uid;
            }else if(type==4){
                path="/friend/releasePost?circleId="+this.circleId+"&uid="+this.uid+"&name="+encodeURI(this.groupInfo.name);
            }else if(type==5){
                path="/friend/circleNotice"
            }else if(type==6){
                path="/userGoodsInfo?productId="+productId+"&uid="+this.uid
            }
            location.href=path;
        },
        getPost:function(postId){
            console.log("999999999999999999")
            var path="/friend/postDetails?postId="+postId+"&uid="+this.uid;
            location.href=path;
        },
        //显示隐藏
        getIsshow:function(){
            this.isShow=!this.isShow;
        },
        getIsClassify:function(num){
            if(num){
                this.getIsScreen();
            }
            this.pop.show=!this.pop.show;
        },
        getIsScreen:function(num,postId){
            this.isScreen=num;
            if(num&&postId){
                this.postId=postId;
            }
            if(num==2){
                this.list.pageNumber=1;
                this.getInfoMediaAdvCommentList();
            }
        },
        //删除帖子
        deletePost:function(){
            var that=this;
            if(that.groupInfo.isMain==1){
                data.delType=2
            }else{
                that.getIsClassify();
                vm.$refs.prompt.toast("无权删除帖子！");
                return false;
            }
            var data={
                loginUid:that.uid,
                postId:that.postId
            };
            console.log(data);
            this.ajax({url:"/friend/deletePost",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.getGroupInfo();
                }else{
                    vm.$refs.prompt.toast("删除帖子失败！");
                }
            });
        },
        //发表评论
        comment:function(){
            var that=this;
            if(!that.commentText){
                vm.$refs.prompt.toast("请输入评论内容！");
                return false;
            }
            var data={
                loginUid:that.uid,
                postId:that.postId,
                content:that.commentText
            };
            console.log(data);
            this.ajax({url:"/friend/postComment",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.commentText="";
                    that.getInfoMediaAdvCommentList();
                    that.getCirclePost();
                    //console.log(b.json.data)
                }else{
                    vm.$refs.prompt.toast("评论失败！");
                }
            });
        },
        //获取自媒体评论列表
        getInfoMediaAdvCommentList:function(){
            var that=this;
            var data={
                loginUid:that.uid,
                postId:that.postId,
                pageNumber:vm.list.pageNumber
            };
            //console.log(data)
            this.ajax({url:"/friend/getInfoPostCommentList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==00&&b.json.data){
                    that.commentList=b.json.data;
                    that.commentList1=that.commentList.list;
                    if(that.commentList.pageNumber>1){
                        if(that.commentList.list.length>0) {
                            that.commentList2 = that.commentList2.concat(b.json.data.list);
                        }
                    }else{
                        that.commentList2= that.commentList1;
                    }
                    vm.loadFlag=false;
                    console.log(that.commentList)
                }else{
                    that.commentList={};
                    that.commentList1=[];
                    that.commentList2=[];
                    vm.$refs.prompt.toast("获取评论列表！");
                }
            });
        },
        //评论点赞
        praiseMediaAdvComment:function(state,commentId){
            var that=this;
            console.log(state+"----"+commentId)
            var data={
                loginUid:that.uid,
                commentId:commentId
            };
            if(state==0){
                data.state=1;
            }else{
                data.state=0;
            }
            console.log(data)
            this.ajax({url:"/friend/praisePostComment",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.commentList2.forEach(function(val){
                        if(val.commentId==commentId){
                            val.praiseState=data.state;
                            val.praiseCount=b.json.data.praiseCount;
                        }
                    });
                    console.log(b.json.data)
                }else{
                    vm.$refs.prompt.toast("点赞失败！");
                }
            });
        },
        //点赞
        getPraise:function(postId,isPraise){
            var that=this;
            var data={
                loginUid:that.uid,
                postId:postId
            };
            if(isPraise==0){
                data.state=1;
            }else{
                data.state=0;
            }
            console.log(data);
            this.ajax({url:"/friend/praisePost",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.circlePost2.forEach(function(val){
                        if(val.postId==postId){
                            val.isPraise=data.state;
                            val.praiseCount=b.json.data.praiseCount;
                        }
                    });
                    console.log(b.json.data)

                }else{
                    vm.$refs.prompt.toast("点赞失败！")
                }
            });
        },
        getCommentInfo:function(commentId){
            location.href="/see/commentInfo?type=2&id="+this.postId+"&loginUid="+this.uid+"&commentId="+commentId;
        },
        getChoice:function(num){
            if(num==1){
                location.href="/friend/choiceList?number=0&postId="+this.postId;
            }else{
                location.href="/friend/forward?number=0&postId="+this.postId;
            }

        }
    }
});
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;
    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
window.onscroll=function(){
    var loadFlag;
    if(vm.isScreen==2){
        loadFlag = vm.commentList.lastPage;
    }else{
        loadFlag = vm.circlePost.lastPage;
    }

    //监听事件内容
    if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
        //当滚动条到底时,这里是触发内容
        if(!loadFlag){
            if(vm.isScreen==2){
                vm.loadFlag=true;
                vm.list.pageNumber++;
                vm.getInfoMediaAdvCommentList();
            }else{
                vm.loadFlag=true;
                vm.list.pageNumber++;
                vm.getCirclePost();
            }
        }else{
            vm.$refs.prompt.toast("没有更多了！")
        }
    }
};